﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using MaxStudio.Concrete.DBUtility;

namespace MaxStudio.Concrete.DBUtility
{
    public class QIMSHttpModule : IHttpModule
    {
        public QIMSHttpModule()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        public void Dispose()
        {

        }

        public void Init(HttpApplication application)
        {
            application.AcquireRequestState += new EventHandler(context_AcquireRequestState);
        }

        void context_AcquireRequestState(object sender, EventArgs e)
        {
            bool isTimeout = true;
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;

            string url = context.Request.Url.AbsoluteUri.ToLower();
            if (url.Contains(".aspx"))
            {
                if (!url.Contains("login.aspx"))
                {
                    if (application.Session != null)
                    {
                        HttpSessionState session = application.Session;
                        HttpResponse response = application.Response;

                        if (context.Session[ConcreteConstants.Session_ExpireTime] != null)
                        {
                            DateTime dtExpireTime = (DateTime)session[ConcreteConstants.Session_ExpireTime];
                            if (DateTime.Now <= dtExpireTime)
                            {
                                isTimeout = false;
                                int minutes = (int)session[ConcreteConstants.Session_ExpireMinutes];
                                dtExpireTime = DateTime.Now.AddMinutes(minutes);
                                session[ConcreteConstants.Session_ExpireTime] = dtExpireTime;
                            }
                        }

                        if (isTimeout)
                        {
                            response.Redirect("~/Login.aspx");
                        }
                    }
                }
            }
        }
    }
}